package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            //因为使用dubbo调用需要进行判断数据是否返回,容易丢失
            Map map = roleService.findAll();
            return new Result(true, MessageConstant.GET_ROLES_LIST_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLES_LIST_FAIL);
        }

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissions, Integer[] menus) {
        try {
            roleService.add(role, permissions, menus);
            return new Result(true, MessageConstant.SET_ROLESMANAGER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SET_ROLESMANAGER_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissions, Integer[] menus) {
        try {
            roleService.edit(role, permissions, menus);
            return new Result(true, MessageConstant.EDIT_ROLESMANAGER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT__ROLESMANAGER_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = roleService.findById(id);
            return new Result(true, MessageConstant.GET_ROLES_LIST_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLES_LIST_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,MessageConstant.DELETE_ROLESMANAGER_SUCCESS);
        } catch(RuntimeException r){
            return new Result(false,r.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLESMANAGER_FAIL);
        }
    }
}
